package com.example.login.Service;

import com.example.login.Mapper.SearchMap;
import com.example.login.Pojo.*;
import com.example.login.Pojo.Color;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.*;
import java.util.ArrayList;

@Service("SearchService")
public class SearchService {

    @Autowired
    SearchMap searchMap;
    public ArrayList<ArrayList<Goods>> searchGoods(String words)
    {

        ArrayList<mp3Goods> mp3Lists;
        ArrayList<mobileGoods> mobileLists;
        ArrayList<PCGoods> pcLists;
        ArrayList<Goods> mp3G;
        ArrayList<Goods> pcG;
        ArrayList<Goods> mobileG;
        ArrayList<Goods> goods = new ArrayList<Goods>();
        //如果搜索词不多,扩大搜索范围
        if(words.length()<4)
        {
            mp3G=searchMap.searchMp3(words);
            pcG=searchMap.searchPc(words);
            mobileG=searchMap.searchMobile(words);
            System.out.println(mp3G);
            System.out.println(pcG);
            System.out.println(mobileG);
        }
        else {
            mp3Lists = searchMap.searchMp3ByItemName(words);
            mobileLists = searchMap.searchMobileByItemName(words);
            pcLists = searchMap.searchPcByItemName(words);
            String color = getColor(words);
            boolean iF = isFrigth0(words);
            System.out.println("color" + color);
            System.out.println("iF" + iF);
            mp3G = filterByColorAndFright(mp3Lists, color, iF);
            pcG = filterByColorAndFright2(pcLists, color, iF);
            mobileG = filterByColorAndFright3(mobileLists, color, iF);

        }
        goods.addAll(pcG);
        goods.addAll(mobileG);
        goods.addAll(mp3G);
        //变成三个每列
        ArrayList<ArrayList<Goods>> lists=new ArrayList<>();
        ArrayList<Goods> temp=new ArrayList<>(3);
        for(int i=0;i<goods.size();i++)
        {
            temp.add(goods.get(i));
            if(i%3==2||i==goods.size()-1)
            {
                lists.add(temp);
                temp=new ArrayList<>(3);
            }
        }
        System.out.println(lists);
        return lists;
    }
    ArrayList<Goods> filterByColorAndFright( ArrayList<mp3Goods> goods,String color,boolean isFright0)
    {
        ArrayList<Goods> g =new ArrayList<Goods>();
        if(isFright0 && color!=null)
        {
            for(int i=0;i<goods.size();i++)
            {
                mp3Goods m=goods.get(i);
                if(m.getFright()==0&&(m.getColor().contains(color)||color.contains(m.getColor())))
                {
                    g.add(((Goods)m));
                }
            }
        }
        else if (isFright0 && color==null)
        {
            for(int i=0;i<goods.size();i++)
            {
                mp3Goods m=goods.get(i);
                if(m.getFright()==0)
                {
                    g.add(((Goods)m));
                }
            }
        }
        else if(color!=null)
        {
            for(int i=0;i<goods.size();i++)
            {
                mp3Goods m=goods.get(i);
                if(m.getColor().contains(color)||color.contains(m.getColor()))
                {
                    g.add(((Goods)m));
                }
            }
        }
        else{
            for(int i=0;i<goods.size();i++)
            {
                mp3Goods m=goods.get(i);
                g.add(((Goods)m));
            }
        }
        return g;
    }
    ArrayList<Goods> filterByColorAndFright2( ArrayList<PCGoods> goods,String color,boolean isFright0)
    {
        ArrayList<Goods> g =new ArrayList<Goods>();
        if(isFright0 && color!=null)
        {
            for(int i=0;i<goods.size();i++)
            {
                PCGoods m=goods.get(i);
                if(m.getFright()==0&&(m.getColor().contains(color)||color.contains(m.getColor())))
                {
                    g.add(((Goods)m));
                }
            }
        }
        else if (isFright0 && color==null)
        {
            for(int i=0;i<goods.size();i++)
            {
                PCGoods m=goods.get(i);
                if(m.getFright()==0)
                {
                    g.add(((Goods)m));
                }
            }
        }
        else if(color!=null)
        {
            for(int i=0;i<goods.size();i++)
            {
                PCGoods m=goods.get(i);
                if(m.getColor().contains(color)||color.contains(m.getColor()))
                {
                    g.add(((Goods)m));
                }
            }
        }
        else{
            for(int i=0;i<goods.size();i++)
            {
                PCGoods m=goods.get(i);
                g.add(((Goods)m));
            }
        }
        return g;
    }
    ArrayList<Goods> filterByColorAndFright3( ArrayList<mobileGoods> goods,String color,boolean isFright0)
    {
        ArrayList<Goods> g =new ArrayList<Goods>();
        if(isFright0 && color!=null)
        {
            for(int i=0;i<goods.size();i++)
            {
                mobileGoods m=goods.get(i);
                if(m.getFright()==0&&(m.getColor().contains(color)||color.contains(m.getColor())))
                {
                    g.add(((Goods)m));
                }
            }
        }
        else if (isFright0 && color==null)
        {
            for(int i=0;i<goods.size();i++)
            {
                mobileGoods m=goods.get(i);
                if(m.getFright()==0)
                {
                    g.add(((Goods)m));
                }
            }
        }
        else if(color!=null)
        {
            for(int i=0;i<goods.size();i++)
            {
                mobileGoods m=goods.get(i);
                if(m.getColor().contains(color)||color.contains(m.getColor()))
                {
                    g.add(((Goods)m));
                }
            }
        }
        else{
            for(int i=0;i<goods.size();i++)
            {
                mobileGoods m=goods.get(i);
                g.add(((Goods)m));
            }
        }
        return g;
    }
    String getColor (String words)
    {
        for (int i=0;i<Color.colors.length;i++)
        {
            if(words.contains(Color.colors[i])&&Color.colors[i]!="")
            {
                return Color.colors[i];
            }
        }
        return null;
    }
    boolean isFrigth0(String words)
    {
        return words.contains("包邮") || words.contains("运费为0");
    }

}
